//
//  SRPersonalHeaderView.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/3/25.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRPersonalHeaderView.h"
#import <FZIconImageView.h>

@interface SRPersonalHeaderView ()
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
@property (weak, nonatomic) IBOutlet FZIconImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;
@property (weak, nonatomic) IBOutlet UILabel *gradeLabel;
@property (weak, nonatomic) IBOutlet UIButton *editButton;

@property (weak, nonatomic) IBOutlet UIImageView *makImageView;


@end


@implementation SRPersonalHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    
    
    self.iconImageView.iconStyle = FZIconImageViewStyleCircle;
    self.iconImageView.borderWidth = 0;
    
    self.makImageView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.45];
    
    self.nicknameLabel.font = css.fontOfH2;
    self.nicknameLabel.textColor = [UIColor whiteColor];
    
    self.gradeLabel.font = css.fontOfH3;
    self.gradeLabel.textColor = [UIColor whiteColor];
    
    [self.editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.editButton setBackgroundImage:[UIImage imageWithColor:css.color_11] forState:UIControlStateNormal];
    self.editButton.layer.cornerRadius = 4;
    self.editButton.layer.masksToBounds = YES;
    [self.editButton addTarget:self action:@selector(onEditButton:) forControlEvents:UIControlEventTouchUpInside];
    
    WEAKSELF
    [self.iconImageView addTapGestureHandler:^(UITapGestureRecognizer *gestureRecognizer, UIView *view, CGPoint locationInView) {
        if (weakSelf.touchIconBlock) {
            weakSelf.touchIconBlock();
        }
    }];
    
    [self.nicknameLabel addTapGestureHandler:^(UITapGestureRecognizer *gestureRecognizer, UIView *view, CGPoint locationInView) {
        if (weakSelf.touchIconBlock) {
            weakSelf.touchIconBlock();
        }
    }];
}

- (void)onEditButton:(id)sender {
    if (self.touchEditButtonBlock) {
        self.touchEditButtonBlock();
    }
}

- (void)resetHeaderViewWithLogin:(BOOL)isLogin {
    if (isLogin) {
        self.nicknameLabel.text = [SRLoginUser nickname];
        [self.iconImageView fz_setImageWithAvatarDefaultPlaceholderWithURL:[NSURL URLWithString:[SRLoginUser avatar]] placeholderSize:CGSizeZero];
        
        self.editButton.hidden = NO;
        self.gradeLabel.hidden = NO;
        
        NSString *gradeString = [SRLoginUser schoolGrade];
        if ([gradeString integerValue] > 0) {
            self.gradeLabel.text = [NSString stringWithFormat:@"%@年级", gradeString];
        } else {
            self.gradeLabel.text = @"";//[[SRLoginManager sharedManager] schoolGrade];
        }
    } else {
        self.nicknameLabel.text = @"点击登录";
        self.iconImageView.image = [UIImage imageNamed:@"default_avatar"];
        self.editButton.hidden = YES;
        self.gradeLabel.hidden = YES;
    }
    
}

- (void)setIconImage:(UIImage *)image {
    self.iconImageView.image = image;
}

@end
